package com.scheduling.schedule.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.scheduling.schedule.entity.WorkOrder;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 工单Mapper接口
 *
 * @author 开发团队
 */
@Mapper
public interface WorkOrderMapper extends BaseMapper<WorkOrder> {

    /**
     * 查询待排产的工单
     *
     * @return 待排产工单列表
     */
    @Select("SELECT * FROM work_order WHERE status = '待排产' ORDER BY priority ASC, create_time ASC")
    List<WorkOrder> selectPendingScheduleOrders();

    /**
     * 根据状态查询工单
     *
     * @param status 工单状态
     * @return 工单列表
     */
    @Select("SELECT * FROM work_order WHERE status = #{status} ORDER BY create_time DESC")
    List<WorkOrder> selectByStatus(@Param("status") String status);

    /**
     * 根据优先级查询工单
     *
     * @param priority 优先级
     * @return 工单列表
     */
    @Select("SELECT * FROM work_order WHERE priority = #{priority} ORDER BY create_time ASC")
    List<WorkOrder> selectByPriority(@Param("priority") Integer priority);

    /**
     * 根据产品ID查询工单
     *
     * @param productId 产品ID
     * @return 工单列表
     */
    @Select("SELECT * FROM work_order WHERE product_id = #{productId} ORDER BY create_time DESC")
    List<WorkOrder> selectByProductId(@Param("productId") String productId);

    /**
     * 根据订单项ID查询工单
     *
     * @param orderItemId 订单项ID
     * @return 工单列表
     */
    @Select("SELECT * FROM work_order WHERE order_item_id = #{orderItemId} ORDER BY create_time DESC")
    List<WorkOrder> selectByOrderItemId(@Param("orderItemId") String orderItemId);

    /**
     * 查询紧急工单（按截止时间和优先级排序）
     *
     * @return 紧急工单列表
     */
    @Select("SELECT * FROM work_order WHERE status = '待排产' AND priority <= 2 ORDER BY priority ASC, plan_end_time ASC")
    List<WorkOrder> selectUrgentOrders();

    /**
     * 按产品分组查询待排产工单
     *
     * @return 工单列表（按产品ID分组排序）
     */
    @Select("SELECT * FROM work_order WHERE status = '待排产' ORDER BY product_id, priority ASC, create_time ASC")
    List<WorkOrder> selectPendingOrdersGroupByProduct();
}